<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------

use think\Db;

// 应用公共文件


/**
 * 记录日志(写入数据库)
 * @param string $content 操作内容
 * @param string $pk  主键
 * @param string $table 数据表名
 * @param string $cur_time 操作时间
 * @param string $uid 操作用户
 */
function do_log($content='操作记录', $pk='', $table='', $cur_time='', $uid=0)
{
    if(empty($cur_time) || !is_numeric($cur_time)){
        $cur_time = time();
    }
    //操作用户
    if(empty($uid) || !is_numeric($uid)){
        //默认后台管理员
        $user_id = cmf_get_current_admin_id();
    }else{
        $user_id = $uid;
    }

    $log['user_id'] = $user_id;
    $log['ip'] = get_client_ip();
    $log['pk'] = $pk;
    $log['table'] = $table;
    $log['create_time'] = $cur_time;
    $log['content'] = $content;

    Db::name('log')->insert($log);
}

/**
 * 记录日志(写入文件)
 * @param $file
 * @param $word
 */
function log_result($file, $word)
{
    $fp = fopen($file,"a");
    flock($fp, LOCK_EX) ;
    fwrite($fp,"执行日期：".strftime("%Y-%m-%d-%H:%M:%S",time())."\n".$word."\n\n");
    flock($fp, LOCK_UN);
    fclose($fp);
}

/**
 * 记录学生入班和退班的操作
 * @param string $content 操作内容
 * @param string $pk  主键
 * @param string $table 数据表名
 * @param string $cur_time 操作时间
 */
function do_log_student_active($content='学生记录',$pk='',$table,$cur_time='',$student_id='0')
{
    if(empty($cur_time) || is_numeric($cur_time))
    {
        $cur_time=time();
    }
    $log['user_id'] = cmf_get_current_admin_id();
    if(empty($log['user_id']))
    {
        $log['user_id'] = cmf_get_current_admin_id();
    }
    $log['ip'] = get_client_ip();
    $log['student_id'] = $student_id;
    $log['pk'] = $pk;
    $log['table'] = $table;
    $log['create_time'] = $cur_time;
    $log['content'] = $content;

    Db::name('log_student_active')->insert($log);

}


/*
 * 按字段对多维数组进行排序
 * @param array  排序数组
 * @param field  排序字段
 * @param asc 排序方式
 * @return array
 */
function sortArrByField(&$array, $field, $asc = true)
{
    $fieldArr = array();
    foreach ($array as $k => $v) {
        $fieldArr[$k] = $v[$field];
    }
    $sort = $asc ? SORT_ASC : SORT_DESC;
    array_multisort($fieldArr, $sort, $array);
}

/* 按照中文 截取字符串
 * 英文都按一个字节，中文都按3个字节计算
 * @param  $string  目标字符串
 * @param  $length  截取长度
 * @param  charset 字符编码
 * @return string
 */
function get_substring($string, $length=15, $etc='…', $charset='utf-8') {
    if (0 == $length) {
        return '';
    }

    //字符串原长度 按照字节判断
    $originalLength = strlen($string);
    $length = $length * 3;//按照汉字占3个字节计算

    if($originalLength > $length){
        $length -= strlen($etc);
        if ($length <= 0) {
            return '';
        }

        $idx = 0;//实际截取长度的字节位置
        for($i=0; $i<$length; $i++){
            //汉字
            if(ord($string[$i]) > 127){
                $i = $i + 2;
                $idx = $idx + 3;
            }else{
                $idx ++;
            }
        }
        return substr($string, 0, $idx) . $etc;
    }

    return $string;
}



